package main

import (
	"encoding/json"
)

type AccessToken struct {
	appid  string
	secret string
}

func NewAccessToken(appid, secret string) *AccessToken {
	return &AccessToken{appid: appid, secret: secret}
}

// GetStableAccessToken 稳定版Token
func (s *AccessToken) GetStableAccessToken() (entity StableAccessTokenResp, err error) {
	url := "https://api.weixin.qq.com/cgi-bin/stable_token"
	params := StableAccessTokenParams{
		Appid:        s.appid,
		Secret:       s.secret,
		GrantType:    "client_credential",
		ForceRefresh: false,
	}

	b, err := json.Marshal(params)
	if err != nil {
		return
	}

	res, err := HttpPostJson(url, b)
	if err != nil {
		return
	}

	err = json.Unmarshal(res, &entity)
	return
}
